{# /rep #}
{% extends "template.html" %}

{% block head %}
<script type="text/javascript" src="/js/prototype.js"></script>
<script type="text/javascript">
function evt_to(e, f){
	e.stop();
	$("to").value += f.down().value+";";
	f.disable();
}
function evt_del(e, f){
	e.stop();
	f.request({onSuccess: function(rep){
		if(rep.headerJSON && rep.headerJSON.ok)
			f.up().remove();
	}});
}
function evt_add(e, f){
	e.stop();
	f.request({onSuccess: function(rep){
		if(rep.headerJSON && rep.headerJSON.err==0){
			$('rep').down().next().insert(mkEntry(rep.headerJSON.nom));
			f.down().clear();
			f.down("span").remove();
		}else if(rep.headerJSON && rep.headerJSON.err==1){
			f.insert(new Element('span'));
			f.down("span").appendChild(
					document.createTextNode("Le nom est trop court"));
		}else{
			console.log(rep.headerJSON.err);
			//location.reload();
		}
	}});
}
function mkEntry(name){
	var li = document.createElement('li');
		li.addClassName('entry');
	var em = $(document.createElement('em'));
		em.appendChild(new Element('a', {href:'/profil/'+name}));
		em.down().appendChild(document.createTextNode(name));
		li.appendChild(em);
	var del = new Element('form', {action:'/rep/del', method:'post'});
	var d_n = new Element('input',{type:'hidden',value:name, name:'name'});
	var d_s = new Element('input',{type:'submit',value:'supprimer'});
		del.appendChild(d_n);
		del.appendChild(d_s);
		li.appendChild(del);
	var wrt = new Element('form', {action:'/rep', method:'get'});
	var w_n = new Element('input',{type:'hidden',value:name, name:'to'});
	var w_s = new Element('input',{type:'submit',value:'écrire'});
		wrt.appendChild(w_n);
		wrt.appendChild(w_s);
		li.appendChild(wrt);
	return li;
}
document.observe("dom:loaded", function(){
	$('rep').on('submit', 'form.del', evt_del);
	$('rep').on('submit', 'form[method="get"]', evt_to);
	$('rep_add').on('submit', evt_add);
});
</script>
{% endblock head %}

{% block conn %}
<div class="part list">
	<fieldset id="rep">
		<legend>Répertoire</legend>
		<ul>
			{% for ppl in contact %}
			<li class="entry"><em><a href="/profil/{{ppl}}"/>{{ppl}}</a></em>
				<form method="post" action="/rep/del" class="del">
					<input type="hidden" name="name" value="{{ppl}}" />
					<input type="submit" value="supprimer" /></form>
				<form method="get" action="/rep" class="wrt">
					<input type="hidden" name="to" value="{{ppl}}" />
					<input type="submit" value="crire" /></form>
			</li>
			{% endfor %}
		</ul>
		<form id="rep_add" method="post" action="/rep/add">
			<input type="text" name="name" />
			<input type="submit" value="+" class="ok" />
		</form>
	</fieldset>
	{%if mailList.own%}<fieldset id="rep">
		<legend>Répertoire</legend>
		<ul>
		</ul>
	</fieldset>{%endif%}
</div>

<div class="part">
	{% include "send_form.html" %}
</div>
{% endblock conn %}
